//
//  GYCartGoodsCell.m
//  GYDemo
//
//  Created by mac on 2018/4/8.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYCartGoodsCell.h"
#import "Head.h"

//@interface GYCartGoodsCell()<GCAdaptLabelDelegate,UITextFieldDelegate>
//@property (nonatomic,strong) UIImageView *signIV;
//@property (nonatomic,strong) UIImageView *imageIV;
//@property (nonatomic,strong) GCAdaptLabel *nameLB;
//@property (nonatomic,strong) GCAdaptLabel *typeLB;;
//@property (nonatomic,strong) UILabel *priceLB;
//@property (nonatomic,strong) UITextField *priceTF;
//@property (nonatomic,strong) UILabel *priceUnitLB;
//@property (nonatomic,strong) UILabel *countLB;
//@property (nonatomic,strong) UITextField *countTF;
//@property (nonatomic,strong) UILabel *countUnitLB;
//@end
//
//@implementation GYCartGoodsCell
//#pragma mark - getter and setter
//-(UIImageView *)signIV
//{
//    if (!_signIV) {
//        _signIV = [UIImageView new];
//        _signIV.frame = WFCGRectMake(0, 0, 18, 18);
//        _signIV.centerX = WFCGFloatX(21);
//        _signIV.centerY = WFCGFloatY(60);
//    }
//    return _signIV;
//}
//-(UIImageView *)imageIV
//{
//    if (!_imageIV) {
//        _imageIV = [UIImageView new];
//        _imageIV.frame = WFCGRectMake(13, 10.5, 100, 100);
//        _imageIV.backgroundColor = Main_Color_BG;
//    }
//    return _imageIV;
//}
//-(GCAdaptLabel *)nameLB
//{
//    if (!_nameLB) {
//        _nameLB = [[GCAdaptLabel alloc]initWithMaxWidth:WFCGFloatX(145.5)];
//        _nameLB.x = WFCGFloatX(124);
//        _nameLB.y = WFCGFloatY(10.5);
//        _nameLB.width = WFCGFloatX(130.5);
//        _nameLB.height = WFCGFloatY(20);
//        _nameLB.font = SYSTEMFONT(14);
//        _nameLB.textColor = Main_Color_Black;
//        _nameLB.delegate = self;
//    }
//    return _nameLB;
//}
//
//-(GCAdaptLabel *)typeLB
//{
//    if (!_typeLB) {
//        _typeLB = [[GCAdaptLabel alloc]initWithMaxWidth:WFCGFloatX(60)];
//        _typeLB.x = WFCGFloatX(259.5);
//        _typeLB.y = WFCGFloatY(13);
//        _typeLB.width = WFCGFloatX(60);
//        _typeLB.height = WFCGFloatY(14);
//        _typeLB.font = SYSTEMFONT(10);
//        _typeLB.textColor = Main_Color_Red;
//        [WFFunctions WFUIaddbordertoView:_typeLB radius:WFCGFloatY(3) width:0.7f color:Main_Color_Red];
//        _typeLB.delegate = self;
//    }
//    return _typeLB;
//}
//
//-(UILabel *)priceLB
//{
//    if (!_priceLB) {
//        _priceLB = [[UILabel alloc]init];
//        _priceLB.x = WFCGFloatX(124);
//        _priceLB.y = WFCGFloatY(47.5);
//        _priceLB.width = WFCGFloatX(50);
//        _priceLB.height = WFCGFloatY(21);
//        _priceLB.textColor = Main_Color_Black;
//        _priceLB.font = SYSTEMFONT(15);
//        _priceLB.text = @"单价";
//    }
//    return _priceLB;
//}
//
//-(UITextField *)priceTF
//{
//    if (!_priceTF) {
//        _priceTF = [UITextField new];
//        _priceTF.frame = WFCGRectMake(173, 43.5, 95, 30);
//        _priceTF.delegate = self;
//        _priceTF.textColor = Main_Color_Black;
//        _priceTF.font = SYSTEMFONT(15);
//        _priceTF.textAlignment = NSTextAlignmentCenter;
//        _priceTF.keyboardType = UIKeyboardTypeNumberPad;
//        [WFFunctions WFUIaddbordertoView:_priceTF radius:0 width:0.7f color:Main_Color_BG];
//    }
//    return _priceTF;
//}
//
//-(UILabel *)priceUnitLB
//{
//    if (!_priceUnitLB) {
//        _priceUnitLB = [[UILabel alloc]init];
//        _priceUnitLB.x = WFCGFloatX(268 + 20);
//        _priceUnitLB.y = WFCGFloatY(47.5);
//        _priceUnitLB.width = WFCGFloatX(60);
//        _priceUnitLB.height = WFCGFloatY(21);
//        _priceUnitLB.textColor = Main_Color_Black;
//        _priceUnitLB.font = SYSTEMFONT(15);
//        _priceUnitLB.text = @"元/";
//    }
//    return _priceUnitLB;
//}
//
//-(UILabel *)countLB
//{
//    if (!_countLB) {
//        _countLB = [[UILabel alloc]init];
//        _countLB.x = WFCGFloatX(124);
//        _countLB.y = WFCGFloatY(82);
//        _countLB.width = WFCGFloatX(50);
//        _countLB.height = WFCGFloatY(21);
//        _countLB.textColor = Main_Color_Black;
//        _countLB.font = SYSTEMFONT(15);
//        _countLB.text = @"数量";
//    }
//    return _countLB;
//}
//
//-(UITextField *)countTF
//{
//    if (!_countTF) {
//        _countTF = [UITextField new];
//        _countTF.frame = WFCGRectMake(173, 78.5, 95, 30);
//        _countTF.delegate = self;
//        _countTF.textColor = Main_Color_Black;
//        _countTF.font = SYSTEMFONT(15);
//        _countTF.textAlignment = NSTextAlignmentCenter;
//        _countTF.keyboardType = UIKeyboardTypeNumberPad;
//        [WFFunctions WFUIaddbordertoView:_countTF radius:0 width:0.7f color:Main_Color_BG];
//    }
//    return _countTF;
//}
//
//-(UILabel *)countUnitLB
//{
//    if (!_countUnitLB) {
//        _countUnitLB = [[UILabel alloc]init];
//        _countUnitLB.x = WFCGFloatX(268 + 20);
//        _countUnitLB.y = WFCGFloatY(82);
//        _countUnitLB.width = WFCGFloatX(50);
//        _countUnitLB.height = WFCGFloatY(21);
//        _countUnitLB.textColor = Main_Color_Black;
//        _countUnitLB.font = SYSTEMFONT(15);
//    }
//    return _countUnitLB;
//}
//-(void)setModel:(GYCartGoodsModel *)model
//{
//    _model = model;
//    NSString *picurl = [NSString stringWithFormat:@"%@%@",_model.picurl,kProductImgThumbnailSuffix];
//    [_imageIV sd_setImageWithURL:[NSURL URLWithString:picurl]];
//    _nameLB.text = _model.name;
//    _typeLB.text = [NSString stringWithFormat:@" %@ ",_model.protypename];
//    _priceTF.text = [NSString stringWithFormat:@"%f",_model.price];
//    _priceUnitLB.text = [NSString stringWithFormat:@"元/%@",_model.unit];
//    _countTF.text = [NSString stringWithFormat:@"%ld",_model.num];
//    _countUnitLB.text = _model.unit;
//}
//#pragma mark - event
//#pragma mark - method
//+(CGFloat)finalHeight
//{
//    return WFCGFloatY(120.5);
//}
//#pragma mark - life
//-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
//{
//    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//        [self.contentView addSubview:self.imageIV];
//        [self.contentView addSubview:self.nameLB];
//        [self.contentView addSubview:self.typeLB];
//        [self.contentView addSubview:self.priceLB];
//        [self.contentView addSubview:self.priceTF];
//        [self.contentView addSubview:self.priceUnitLB];
//        [self.contentView addSubview:self.countLB];
//        [self.contentView addSubview:self.countTF];
//        [self.contentView addSubview:self.countUnitLB];
//    }
//    return self;
//}
//
//-(void)layoutSubviews
//{
//    [super layoutSubviews];
//    _typeLB.x = _nameLB.right + WFCGFloatX(5);
//}
//
//- (void)awakeFromNib {
//    [super awakeFromNib];
//    // Initialization code
//}
//
//- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    [super setSelected:selected animated:animated];
//
//    // Configure the view for the selected state
//}
//#pragma mark - delegate
//-(void)callback:(id)sender
//{
//    [self layoutSubviews];
//}
//#pragma mark - tf delegate
//-(void)textFieldDidEndEditing:(UITextField *)textField
//{
//    if (textField.text.length > 0) {
//        NSString *value = textField.text;
//        NSString *key;
//        if (textField == _priceTF) {
//            key = @"price";
//        }
//        if (textField == _countTF) {
//            key = @"num";
//        }
//        _callback(_model,value,key);
//    }
//}

@interface GYCartGoodsCell()<GCAdaptLabelDelegate,UITextFieldDelegate>
@property (nonatomic,strong) UIImageView *signIV;
@property (nonatomic,strong) UILabel *companyNameLB;
@property (nonatomic,strong) UIImageView *imageIV;
@property (nonatomic,strong) UILabel *nameLB;
@property (nonatomic,strong) GCAdaptLabel *typeLB;
@property (nonatomic,strong) UILabel *priceLB;
@property (nonatomic,strong) UIView *countV;
@property (nonatomic,strong) UITextField *countTF;
@property (nonatomic,strong) UILabel *countUnitLB;
@end

@implementation GYCartGoodsCell
#pragma mark - getter and setter
-(UIImageView *)signIV
{
    if (!_signIV) {
        _signIV = [UIImageView new];
        _signIV.frame = WFCGRectMake(14, 15, 17, 17);
    }
    return _signIV;
}

-(UILabel *)companyNameLB
{
    if (!_companyNameLB) {
        _companyNameLB = [[UILabel alloc]init];
        _companyNameLB.frame = WFCGRectMake(44, 13, 200, 20);
        _companyNameLB.font = SYSTEMBOLDFONT(14);
        _companyNameLB.textColor = Main_Color_Black;
    }
    return _companyNameLB;
}

-(UIImageView *)imageIV
{
    if (!_imageIV) {
        _imageIV = [UIImageView new];
        _imageIV.frame = WFCGRectMake(44, 50, 90, 90);
        _imageIV.backgroundColor = Main_Color_BG;
    }
    return _imageIV;
}
-(UILabel *)nameLB
{
    if (!_nameLB) {
        _nameLB = [[UILabel alloc]init];
        _nameLB.x = WFCGFloatX(149);
        _nameLB.y = WFCGFloatY(50);
        _nameLB.width = WFCGFloatX(212);
        _nameLB.height = WFCGFloatY(33);
        _nameLB.font = SYSTEMBOLDFONT(13);
        _nameLB.textColor = Main_Color_Black;
    }
    return _nameLB;
}

-(GCAdaptLabel *)typeLB
{
    if (!_typeLB) {
        _typeLB = [[GCAdaptLabel alloc]initWithMaxWidth:WFCGFloatX(60)];
        _typeLB.x = WFCGFloatX(149);
        _typeLB.y = WFCGFloatY(88);
        _typeLB.width = WFCGFloatX(60);
        _typeLB.height = WFCGFloatY(15);
        _typeLB.font = SYSTEMFONT(10);
        _typeLB.textColor = Main_Color_Gray;
        [WFFunctions WFUIaddbordertoView:_typeLB radius:_typeLB.height / 2 width:0.7f color:Main_Color_Gray];
        _typeLB.delegate = self;
    }
    return _typeLB;
}

-(UILabel *)priceLB
{
    if (!_priceLB) {
        _priceLB = [[UILabel alloc]init];
        _priceLB.x = WFCGFloatX(149);
        _priceLB.y = WFCGFloatY(115);
        _priceLB.width = WFCGFloatX(100);
        _priceLB.height = WFCGFloatY(25);
        _priceLB.textColor = HEXCOLOR(@"#FC1B36");
        _priceLB.font = SYSTEMBOLDFONT(15);
    }
    return _priceLB;
}

-(UIView *)countV
{
    if (!_countV) {
        _countV = [UIView new];
        _countV.frame = WFCGRectMake(292, 118, 70, 25);
        _countV.backgroundColor = Main_Color_BG;
        [_countV addSubview:self.countTF];
        [_countV addSubview:self.countUnitLB];
    }
    return _countV;
}

-(UITextField *)countTF
{
    if (!_countTF) {
        _countTF = [UITextField new];
        _countTF.frame = WFCGRectMake(7, 0, 70 - 7 - 5 - 17, 25);
        _countTF.delegate = self;
        _countTF.textColor = Main_Color_Gray;
        _countTF.font = SYSTEMFONT(12);
        _countTF.keyboardType = UIKeyboardTypeNumberPad;
        _countTF.enabled=NO;
    }
    return _countTF;
}

-(UILabel *)countUnitLB
{
    if (!_countUnitLB) {
        _countUnitLB = [[UILabel alloc]init];
        _countUnitLB.x = self.countTF.right;
        _countUnitLB.y = self.countTF.y;
        _countUnitLB.width = WFCGFloatX(17);
        _countUnitLB.height = WFCGFloatY(25);
        _countUnitLB.textColor = Main_Color_LightGray;
        _countUnitLB.font = SYSTEMFONT(12);
    }
    return _countUnitLB;
}
-(void)setModel:(GYCartShopModel *)model
{
    _model = model;
    UIImage *img = [UIImage imageNamed:self.model.is_selected ? @"Selected" : @"Unselected"];
    _signIV.image = img;
    _companyNameLB.text = _model.companyname;
    [_imageIV sd_setImageWithURL:[NSURL URLWithString:self.model.imgpath]];
    _nameLB.text = self.model.name;
    _typeLB.text = [NSString stringWithFormat:@" %@ ",self.model.sizes];
    NSString *units = [NSString stringWithFormat:@"/%@",@"件"];
    NSString *prices = [NSString stringWithFormat:@"￥%@%@",self.model.price,units];
    NSMutableAttributedString *mastr = [[NSMutableAttributedString alloc]initWithString:prices];
    [mastr addAttribute:NSFontAttributeName value:SYSTEMFONT(12) range:NSMakeRange(0, 1)];
    [mastr addAttribute:NSFontAttributeName value:SYSTEMFONT(12) range:NSMakeRange([prices rangeOfString:units].location - 2, [prices rangeOfString:units].length + 2)];
    _priceLB.attributedText = mastr;
    _countTF.text = [NSString stringWithFormat:@"%ld",self.model.count];
    _countUnitLB.text = @"件";
}
#pragma mark - event
#pragma mark - method
+(CGFloat)finalHeight
{
    return WFCGFloatY(164) + 5;
}
#pragma mark - life
-(void)setFrame:(CGRect)frame
{
    frame.size.height -= 5;
    [super setFrame:frame];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.signIV];
        [self.contentView addSubview:self.companyNameLB];
        [self.contentView addSubview:self.imageIV];
        [self.contentView addSubview:self.nameLB];
        [self.contentView addSubview:self.typeLB];
        [self.contentView addSubview:self.priceLB];
        [self.contentView addSubview:self.countV];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    _typeLB.x = WFCGFloatX(149);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
#pragma mark - delegate
-(void)callback:(id)sender
{
    [self layoutSubviews];
}
#pragma mark - tf delegate
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.text.length > 0) {
        NSString *value = textField.text;
        NSString *key;
        if (textField == _countTF) {
            key = @"num";
        }
        if (self.callback) {
            self.callback(self.model,value,key);
        }
    }
}
@end
